package co.wizardattack.model;

//import co.wizardattack.connection.MessageDistpacher;
import co.wizardattack.constants.ResourcesNames;

import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;

import java.io.IOException;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.font.FontManager;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import communication.MessageDistpacher;

import protocol.OnNewMessageListener;
import protocol.Print;
import protocol.RoomConnection;
import protocol.User;
import protocol.XmlParser;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.util.Log;

public class Game implements IOnSceneTouchListener, OnNewMessageListener {

	/**
	 * Constantes
	 */
	private static int CAMERA_WIDTH = 1080;
	private static int CAMERA_HEIGHT = 720;
	/**
	 * Atributos
	 */
	private Camera cameraGame;
	private Scene sceneGame;
	private Scenary scenaryGame;
	private Wizard wizardGame;		
	private Font miFuente;
	private Text miTexto;	
	private Media media;
	private int cuenta = 0;
	private float factorDeMovimiento = 50;

	//Conexion
	private RoomConnection room;

	//Wizard remoto
	private Volcano volcanoRemote;

	/**
	 * Constructor
	 */
	public Game() {
		MessageDistpacher.INSTANCE.setListener(this);
	}

	/**
	 * Metodos
	 */

	@Override
	public void onCloseConnection() {
		// Do something with BattleActivity
	}

	public Camera createCamera() {
		cameraGame = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return cameraGame;
	}

	public void onCreateResources(TextureManager textureManager, FontManager fontManager, AssetManager assetManager, Engine engine, Context context) throws IOException {		
		//SONIDO
		media = new Media(engine.getSoundManager(), engine.getMusicManager(), context);

		//ESCENARIO
		scenaryGame = new Scenary();
		scenaryGame.onCreateResuercesScenary(textureManager, context);

		//WIZARD
		wizardGame = new Wizard();
		wizardGame.onCreateResourcesWizard(textureManager, context);
		//WIZARD REMOTE
		volcanoRemote = new Volcano();
		volcanoRemote.onCreateResourcesVolcano(textureManager, context);


		//FUENTES
		FontFactory.setAssetBasePath(ResourcesNames.FUENTES_PATH);
		final ITexture fontTexture = new BitmapTextureAtlas(textureManager, 256, 256, TextureOptions.BILINEAR);
		miFuente = FontFactory.createFromAsset(fontManager, fontTexture, assetManager, ResourcesNames.FUENTE_NOMBRE, 70, true, Color.BLACK);
		miFuente.load();	
	}

	public Scene onCreateScene(VertexBufferObjectManager vertexBufferObjectManager) {
		//MUSICA
		media.playMusic();

		//ESCENARIO
		scenaryGame.onCreateSceneScenary(vertexBufferObjectManager);

		//WIZARD
		wizardGame.onCreateSceneWizard(vertexBufferObjectManager);
		wizardGame.animatedRight();

		//Volcano REMOTE
		volcanoRemote.onCreateSceneVolcano(vertexBufferObjectManager);
		volcanoRemote.getSpriteAnimatedVolcano().setVisible(false);
		volcanoRemote.animated();

		///TEXTO
		miTexto = new Text(cameraGame.getCenterX(), cameraGame.getCenterX()+100, miFuente, "Score:0123456789", vertexBufferObjectManager);
		miTexto.setText("Score: " + cuenta);

		//SCENE
		sceneGame = new Scene();
		sceneGame.setOnSceneTouchListener(this);
		sceneGame.attachChild(scenaryGame.getSpriteScenary());	
		sceneGame.attachChild(wizardGame.getSpriteAnimatedWizard());
		sceneGame.attachChild(volcanoRemote.getSpriteAnimatedVolcano());
		sceneGame.attachChild(miTexto);
		return sceneGame;
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		if(pSceneTouchEvent.isActionDown()){
			//media.playEffect(media.getEffectFire());			
			cuenta++;
			miTexto.setText("Score: " + cuenta);
			//			if(pairedConnection != null){ ------------------------------
			//				if(wizardGame.getPosX() > cameraGame.getWidth()){
			//					factorDeMovimiento*=-1;
			//					wizardGame.animatedLeft();
			//				}
			//				if(wizardGame.getPosX()< 0){
			//					factorDeMovimiento*=-1;
			//					wizardGame.animatedRight();
			//				}
			//				wizardGame.setPosX(wizardGame.getPosX()+ factorDeMovimiento);
			////				pairedConnection.sendMessage("<x>"+((int)wizardGame.getPosX())+"</x>"); ------------------------------
			//			}
		}
		return false;
	}

	@Override
	public int onNewMessage(String message, User sender) {
		// Receiving a message from the other player
		Print.print("wizardgame",message);
		//Coordenadas del otro Wizard

		try{			
			//cuenta = Integer.valueOf(message);
			Log.i("Aljo", "VolcanoPosX: "+volcanoRemote.getPosX());
			volcanoRemote.setPosX(Integer.valueOf(XmlParser.readByTag(message, "x", 0)));	
			Log.i("Aljo", "Volcano XmlParserTag: "+XmlParser.readByTag(message, "x", 0));
			volcanoRemote.getSpriteAnimatedVolcano().setVisible(true);
			volcanoRemote.animated();
			media.playEffect(media.getEffectVolcano());
		} catch(Exception e) {


		}

		if(volcanoRemote.getPosX() > cameraGame.getWidth()){
			volcanoRemote.animated();		
		}
		if(wizardGame.getPosX()< 0){			
			volcanoRemote.animated();			
		}

		return message.length();
	}

	@Override
	public void getRoomConnection(RoomConnection connection) {
		// TODO Auto-generated method stub

	}
}
